home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.5 / site-packages / debconf.py < prev    next >
Text File  |  2009-10-02  |  6KB  |  185 lines

  1. # Copyright:
  2. #   Moshe Zadka (c) 2002
  3. #   Canonical Ltd. (c) 2005 (DebconfCommunicator)
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. # 1. Redistributions of source code must retain the above copyright
  9. #    notice, this list of conditions and the following disclaimer.
  10. # 2. Redistributions in binary form must reproduce the above copyright
  11. #    notice, this list of conditions and the following disclaimer in the
  12. #    documentation and/or other materials provided with the distribution.
  13. # THIS SOFTWARE IS PROVIDED BY AUTHORS AND CONTRIBUTORS ``AS IS'' AND
  14. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
  17. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. # SUCH DAMAGE.
  24.  
  25. import sys, os
  26. import errno
  27. import re
  28. try:
  29.     import subprocess
  30.     using_subprocess = True
  31. except ImportError:
  32.     import popen2
  33.     using_subprocess = False
  34. import fcntl
  35.  
  36. class DebconfError(Exception):
  37.     pass
  38.  
  39. LOW, MEDIUM, HIGH, CRITICAL = 'low', 'medium', 'high', 'critical'
  40.  
  41. class Debconf:
  42.  
  43.     def __init__(self, title=None, read=None, write=None):
  44.         for command in ('capb set reset title input beginblock endblock go get'
  45.                         ' register unregister subst fset fget previous_module'
  46.                         ' visible purge metaget exist version settitle'
  47.                         ' info progress data').split():
  48.             self.setCommand(command)
  49.         self.read = read or sys.stdin
  50.         self.write = write or sys.stdout
  51.         sys.stdout = sys.stderr
  52.         self.setUp(title)
  53.  
  54.     def setUp(self, title):
  55.         self.version = self.version(2)
  56.         if self.version[:2] != '2.':
  57.             raise DebconfError(256, "wrong version: %s" % self.version)
  58.         self.capabilities = self.capb().split()
  59.         if title:
  60.             self.title(title)
  61.  
  62.     def setCommand(self, command):
  63.         setattr(self, command,
  64.                lambda *args, **kw: self.command(command, *args, **kw))
  65.  
  66.     def command(self, command, *params):
  67.         command = command.upper()
  68.         self.write.write("%s %s\n" % (command, ' '.join(map(str, params))))
  69.         self.write.flush()
  70.  
  71.         while True:
  72.             try:
  73.                 resp = self.read.readline().rstrip('\n')
  74.                 break
  75.             except IOError, e:
  76.                 if e.errno == errno.EINTR:
  77.                     continue
  78.                 else:
  79.                     raise
  80.  
  81.         if ' ' in resp:
  82.             status, data = resp.split(' ', 1)
  83.         else:
  84.             status, data = resp, ''
  85.         status = int(status)
  86.         if status == 0:
  87.             return data
  88.         elif status == 1:   # unescaped data
  89.             unescaped = ''
  90.             for chunk in re.split(r'(\\.)', data):
  91.                 if chunk.startswith('\\') and len(chunk) == 2:
  92.                     if chunk[1] == 'n':
  93.                         unescaped += '\n'
  94.                     else:
  95.                         unescaped += chunk[1]
  96.                 else:
  97.                     unescaped += chunk
  98.             return unescaped
  99.         else:
  100.             raise DebconfError(status, data)
  101.  
  102.     def stop(self):
  103.         self.write.write('STOP\n')
  104.         self.write.flush()
  105.  
  106.     def forceInput(self, priority, question):
  107.         try:
  108.             self.input(priority, question)
  109.             return 1
  110.         except DebconfError, e:
  111.             if e.args[0] != 30:
  112.                 raise
  113.         return 0
  114.  
  115.     def getBoolean(self, question):
  116.         result = self.get(question)
  117.         return result == 'true'
  118.  
  119.     def getString(self, question):
  120.         return self.get(question)
  121.  
  122.  
  123. class DebconfCommunicator(Debconf, object):
  124.     def __init__(self, owner, title=None, cloexec=False):
  125.         args = ['debconf-communicate', '-fnoninteractive', owner]
  126.         if using_subprocess:
  127.             self.dccomm = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
  128.             read = self.dccomm.stdout
  129.             write = self.dccomm.stdin
  130.         else:
  131.             self.dccomm = popen2.Popen3(args)
  132.             read = self.dccomm.fromchild
  133.             write = self.dccomm.tochild
  134.         super(DebconfCommunicator, self).__init__(title=title,
  135.                                                   read=read,
  136.                                                   write=write)
  137.         if cloexec:
  138.             fcntl.fcntl(self.read.fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  139.             fcntl.fcntl(self.write.fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  140.  
  141.     def shutdown(self):
  142.         if self.dccomm is not None:
  143.             if using_subprocess:
  144.                 self.dccomm.stdin.close()
  145.                 self.dccomm.stdout.close()
  146.             else:
  147.                 self.dccomm.tochild.close()
  148.                 self.dccomm.fromchild.close()
  149.             self.dccomm.wait()
  150.             self.dccomm = None
  151.  
  152.     # Don't rely on this; call .shutdown() explicitly.
  153.     def __del__(self):
  154.         try:
  155.             self.shutdown()
  156.         except AttributeError:
  157.             pass
  158.  
  159.  
  160. if ('DEBCONF_USE_CDEBCONF' in os.environ and
  161.     os.environ['DEBCONF_USE_CDEBCONF'] != ''):
  162.     _frontEndProgram = '/usr/lib/cdebconf/debconf'
  163. else:
  164.     _frontEndProgram = '/usr/share/debconf/frontend'
  165.  
  166. def runFrontEnd():
  167.     if 'DEBIAN_HAS_FRONTEND' not in os.environ:
  168.         os.environ['PERL_DL_NONLAZY']='1'
  169.         os.execv(_frontEndProgram, [_frontEndProgram, sys.executable]+sys.argv)
  170.  
  171.  
  172. if __name__ == '__main__':
  173.     runFrontEnd()
  174.     db = Debconf()
  175.     db.forceInput(CRITICAL, 'bsdmainutils/calendar_lib_is_not_empty')
  176.     db.go()
  177.     less = db.getBoolean('less/add_mime_handler')
  178.     aptlc = db.getString('apt-listchanges/email-address')
  179.     db.stop()
  180.     print db.version
  181.     print db.capabilities
  182.     print less
  183.     print aptlc
  184.